<?php
require_once($_SERVER['DOCUMENT_ROOT']."/configures/global.php");
require_once($_SERVER['DOCUMENT_ROOT']."/model/data_access.php");
require_once("helper.php");
function get_nearest_hotels($hotel_uuid)
{
        /**
        *   Function name   Purpose 
        *   update_city     Update specific city record name and country.
        *   
        *   params     description
        *   $cid       Cities id . 
        *   $country   New country name
        *   $city      New city name
        *   $lan       Languages to update (cn or en)
        *   $conn      PDO connection instance
        **/
    $pre_instance = NULL;
    $next_instance = NULL;
    $hotel = get_hotel($hotel_uuid,get_lan());
    $hotels = get_hotels($hotel["CityName_en"],$hotel["CountryName_en"],$hotel["ContinentName_en"],NULL,-1,NULL,-1,get_lan());
    foreach ($hotels as $index=>$current_instance)
    {
        if ($current_instance["HotelWCUUID"] == $hotel_uuid)
        {
            if (($index+1) < count($hotels))
            {
                $next_instance=$hotels[$index+1];
            }
            if($index != 0)
            {
                $pre_instance=$hotels[$index-1];
            }
            break;
        }
    }
    return [$pre_instance,$next_instance];
}
/*Optional hotel_filter , hotel_service and rating */
function get_hotels_data($continent,$country,$city,$page=1,$condition=[])
{
    /**
    *   Function name       Purpose 
    *   get_hotels_data     Return hotel records that matched given condition ,total record count , total page number , rating count , service count.
    *                       Careful!All get_xxxx should be return array[] , first one is paged database data , second is total number of record , third is number of page count .
    *   
    *   params              description
    *   $continent          Continent name
    *   $country            Country name
    *   $city               City name
    *   $page               Page to request
    *   $condition          Optionally condition , available option : hotel_service, hotel_rating
    **/
    $service_fil = array_key_exists("hotel_service",$condition)?$condition["hotel_service"]:-1;
    $rating_fil = array_key_exists("hotel_rating",$condition)?$condition["hotel_rating"]:NULL;
    $hotel_count = 0;
    $rating_group = [];
    $service_group = [];
    $hotel_data = get_hotels($city,$country,$continent,$rating_fil,$service_fil,NULL,$page,get_lan(),$hotel_count,$rating_group,$service_group);
    $page_count = $hotel_count % PAGE_SIZE == 0?$hotel_count/PAGE_SIZE:($hotel_count/ PAGE_SIZE)+1;
    $hotel_subset = array_map(function($row){return [$row["EntityLatitude"],$row["EntityLongitude"]];},$hotel_data);
    return [$hotel_data,$hotel_count,(int)$page_count,$rating_group,$service_group];
}
/*Condition is associative array , difference category require difference key 
    Be careful , do not overlapping key between difference sub category 
*/

function all_accommodation($continent,$country,$city,$page,$condition=[])
{
    /**
    *   Function name       Purpose 
    *   all_accommodation   Return all accommodation;
    *   params              description
    *   $continent          Continent name
    *   $country            Country name
    *   $city               City name
    *   $page               Page to request
    *   $condition          Optionally condition , available option : hotel_service, hotel_rating
    *   Result is one associative array , each key is described below
    *   Key              Description
    *   Records          Array that contain record come from get_xxxx , [HOTEL,B2B] ... and so on.
    *   Total            Total number of record
    *   Category         This category name
    *   Filter           Filter page name used in searching_result.php , If filter is name "abc" , searching_result.php will attempted include abc.filter.php 
    *   Visual           Visual page name used in searching_result.php , If filter is name "abc" , searching_result.php will attempted include abc.visual.php 
    *   Enable_Img       Dirty way to define which image to using if category active
    *   Disable_Img      Dirty way to define which image to using if category not active
    **/
    $hotel_record = get_hotels_data($continent,$country,$city,$page,$condition);
    return ["Records"=>[[$hotel_record,"Sub_type"=>"HOTEL"]],"Total"=> $hotel_record[1],"Category_ID"=>Categories::ACCOMMODATION,"Category"=>Categories::getCategorieName(Categories::ACCOMMODATION,get_lan()),"Filter"=>Categories::getCategorieName(Categories::ACCOMMODATION,"en"),"Visual"=>Categories::getCategorieName(Categories::ACCOMMODATION,"en"),"Enable_Img"=>"/res/img/search_result/search_icons_color_01.gif","Disable_Img"=>"/res/img/search_result/search_icons_gray_01.gif"];
}
//Dummy function
function all_transport($continent,$country,$city,$page,$condition=[])
{
    return ["Total"=>0,"Category"=>Categories::getCategorieName(Categories::TRANSPORT,get_lan()),"Category_ID"=>Categories::TRANSPORT,"Filter"=>Categories::getCategorieName(Categories::TRANSPORT,"en"),"Visual"=>Categories::getCategorieName(Categories::TRANSPORT,"en"),"Enable_Img"=>"/res/img/search_result/search_icons_color_02.gif","Disable_Img"=>"/res/img/search_result/search_icons_gray_02.gif"];
}
function all_shopping($continent,$country,$city,$page,$condition=[])
{
    return ["Total"=>0,"Category"=>Categories::getCategorieName(Categories::SHOPPING,get_lan()),"Category_ID"=>Categories::SHOPPING,"Filter"=>Categories::getCategorieName(Categories::SHOPPING,"en"),"Visual"=>Categories::getCategorieName(Categories::SHOPPING,"en"),"Enable_Img"=>"/res/img/search_result/search_icons_color_03.gif","Disable_Img"=>"/res/img/search_result/search_icons_gray_03.gif"];
}
function all_attraction($continent,$country,$city,$page,$condition=[])
{
    return ["Total"=>0,"Category"=>Categories::getCategorieName(Categories::ATTRACTION,get_lan()),"Category_ID"=>Categories::ATTRACTION,"Filter"=>Categories::getCategorieName(Categories::ATTRACTION,"en"),"Visual"=>Categories::getCategorieName(Categories::ATTRACTION,"en"),"Enable_Img"=>"/res/img/search_result/search_icons_color_04.gif","Disable_Img"=>"/res/img/search_result/search_icons_gray_03.gif"];
}
function all_food($continent,$country,$city,$page,$condition=[])
{
    return ["Total"=>0,"Category"=>Categories::getCategorieName(Categories::FOOD,get_lan()),"Category_ID"=>Categories::FOOD,"Filter"=>Categories::getCategorieName(Categories::FOOD,"en"),"Visual"=>Categories::getCategorieName(Categories::FOOD,"en"),"Enable_Img"=>"/res/img/search_result/search_icons_color_05.gif","Disable_Img"=>"/res/img/search_result/search_icons_gray_04.gif"];
}
//End dummy

//-1 for all 1 for accommodation , 2 for transport , 3 for shopping , 4 for attraction , 5 food ;
function category_selection($category,$continent,$country,$city,$page,$condition=[])
{
    //If attribute <= 0 , it will not display to client. 
    $result = [];
    $result[Categories::ACCOMMODATION] = all_accommodation($continent,$country,$city,$page,$condition);
    $result[Categories::TRANSPORT] = all_transport($continent,$country,$city,$page,$condition);
    $result[Categories::SHOPPING] = all_shopping($continent,$country,$city,$page,$condition);
    $result[Categories::ATTRACTION] = all_attraction($continent,$country,$city,$page,$condition);
    $result[Categories::FOOD] = all_food($continent,$country,$city,$page,$condition);
    if (!$category->is_all())
    {
        $result[$category->getCurrentCategorieID()]["target"] = True;
    }
    usort($result,function($category_a,$category_b)
    {
        if ($category_a["Total"] == $category_b["Total"])
        {
            return 0;
        }
        else
        {
            if (array_key_exists("target",$category_a))
            {
                return -1;
            }
            else if (array_key_exists("target",$category_b))
            {
                return 1;
            }
            else
            {
                return $category_a["Total"] > $category_b["Total"]?-1:1;
            }
        }
    });
    return $result;
}
?>